<template>
  <div class="login-wrap">
    <div class="ms-title">用户登录</div>
    <div class="ms-login">
      <el-form :model="loginForm" :rules="ruleForm">
        <el-form-item label="用户名" prop="name">
          <el-input v-model="loginForm.name" placeholder="请输入用户名" :prefix-icon="Avatar"/>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="loginForm.password" placeholder="请输入密码" type="password" :prefix-icon="Key"/>
        </el-form-item>
        <div class="login-but">
          <el-button  type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="goRegister">注册</el-button>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
import {ref,reactive,getCurrentInstance} from 'vue'
import {ElMessage} from "element-plus";
import {useRouter} from 'vue-router'
import {useStore} from 'vuex'
import { Avatar,Key } from '@element-plus/icons-vue'
export default {
name: "Login",
  components:{
    Avatar,Key
  },
  setup(){
  const ruleForm= reactive({
      name: [
        { required: true, trigger: 'blur' , message: '请输入用户名'}
      ],
      password: [
      { required: true, trigger: 'blur', message: '请输入密码' }
    ],
  })
    const loginForm=reactive({
      name:'',
      password:''
    })
    //获取组件实例
    const { appContext } = getCurrentInstance();
    const api = appContext.config.globalProperties.$API;
    const router=useRouter()
    const store=useStore()
    const login=async ()=>{
      const result=await api.reqLogin(loginForm.name,loginForm.password)
      if (result.status===10000){
       ElMessage({
         showClose: true,
         message: '登录成功！',
         type: 'success',
       })
        store.commit('GETUSERINFO',result.data)
        store.commit('CHANGECHECK',false)
        await router.push('/home')
      }else {
        ElMessage.error('系统异常')
      }
    }
    const goRegister=()=>{
      router.push('/register')
    }
    return{ruleForm,loginForm,login,goRegister,Avatar,Key}
  }
}
</script>

<style scoped>
.login-wrap {
  position: relative;
  background: url("../assets/img/back.jpg") fixed center;
  background-size: cover;
  width: 100%;
  height:695px;
}
.ms-title {
  position: absolute;
  top: 50%;
  width: 100%;
  margin-top: -230px;
  text-align: center;
  font-size: 30px;
  font-weight: 600;
  color: black;
}
.ms-login{
  position:absolute;
  left: 50%;
  top: 50%;
  width: 320px;
  height: 170px;
  margin-left:-190px;
  margin-top: -150px;
  background: #fff;
  padding: 40px;
  border-radius: 5px;
}
.login-but{
  margin-top: 25px;
  display: flex;
}
.login-but button{
  width: 50%;
  height: 36px;
}
</style>
